<template>
  <view class="game-container">
    <!-- 背景图片 -->
    <image
      class="background-image"
      :src="url('fullbg', '', 'png')"
      mode="aspectFill"
    ></image>

    <!-- 添加标题 -->
    <view class="page-title">
      <text class="title-text">投石击破水中天</text>
    <view class="uni-my-2"><text class="title-text">涟漪散作满池圆</text></view>  
    </view>

    <!-- 游戏卡片容器 -->
    <view class="cards-container">
      <view
        v-for="(game, index) in games"
        :key="index"
        class="game-card"
        :style="getCardStyle(index)"
        @click="navigateToGame(game.path)"
      >
        <image :src="url('gameicon',index,'jpg')" mode="aspectFill" class="card-image"></image>
        <text class="card-title uni-error">{{ game.title }}</text>
        <!-- <text class="card-description ti">{{ game.description }}</text> -->
      </view>
    </view>
  </view>
</template>

<script setup>
import { ref, onMounted, onUnmounted } from "vue";
const url = $url;
// 游戏数据
const games = ref([
  {
    title: "消消乐",
    image: "/static/images/games/xiaoxiaole.png",
    path: "/pages/other/physicsgames/delate",
    description:
      "消消乐是一款休闲益智游戏，玩家需要通过消除相同的物理元素来获得分数。游戏画面简洁，操作简单，适合各个年龄段的玩家。",
  },
  {
    title: "DIY填色",
    image: "/static/images/games/diy.png",
    path: "/pages/other/mask",
    description:
      "DIY填色是一款休闲益智游戏，玩家需要通过填色来获得分数。游戏画面简洁，操作简单，适合各个年龄段的玩家。",
  },
  {
    title: "物理学家知多少",
    image: "/static/images/games/yang.png",
    path: "/pages/other/physicsgames/sheep",
    description:
      "物理学家知多少是一款休闲益智游戏，玩家需要通过消除相同的物理元素来获得分数。游戏画面简洁，操作简单，适合各个年龄段的玩家。",
  },
]);

// 当前选中的卡片索引
const currentIndex = ref(0);

// 计算卡片位置和旋转角度
const getCardStyle = (index) => {
  const totalCards = games.value.length;
  const radius = 300; // 扇形半径
  const angleSpan = 60; // 扇形跨度角度
  const startAngle = -angleSpan / 2; // 起始角度
  const angleStep = angleSpan / (totalCards - 1); // 每张卡片之间的角度
  const angle = startAngle + index * angleStep;
  const radian = (angle * Math.PI) / 180;

  const x = radius * Math.sin(radian);
  const y = -radius * Math.cos(radian);

  return {
    transform: `translate(${x}px, ${y}px) rotate(${angle}deg)`,
    zIndex: index === currentIndex.value ? 10 : 1,
    opacity: index === currentIndex.value ? 1 : 0.8,
  };
};

// 自动轮播
let timer = null;
const startAutoRotate = () => {
  timer = setInterval(() => {
    currentIndex.value = (currentIndex.value + 1) % games.value.length;
  }, 3000);
};

// 跳转到游戏页面
const navigateToGame = (path) => {
  uni.navigateTo({
    url: path,
  });
};

onMounted(() => {
  startAutoRotate();
});

// 组件销毁时清除定时器
onUnmounted(() => {
  if (timer) {
    clearInterval(timer);
  }
});
</script>

<style scoped>
.game-container {
  width: 100vw;
  height: 100vh;
  position: relative;
  overflow: hidden;
}

.background-image {
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  z-index: 0;
}

.page-title {
  position: absolute;
  top: 25%;
  left: 50%;
  transform: translateX(-50%);
  text-align: center;
  z-index: 1;
}

.title-text {
  font-size: 36rpx;
  color: #333;
  font-weight: bold;
  text-shadow: 2rpx 2rpx 4rpx rgba(0, 0, 0, 0.1);
  background: linear-gradient(45deg, #6c8380, #8a9a97);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  padding: 20rpx 40rpx;
  border-radius: 20rpx;

 
}

.cards-container {
  position: absolute;
  top: 70%;
  left: 50%;
  transform: translateX(-50%);
  width: 100%;
  height: 400px;
  display: flex;
  justify-content: center;
  align-items: center;
}

.game-card {
  position: absolute;
  width: 200rpx;
  height: 300rpx;
  background: var(--td-bg-color);
  border-radius: 20rpx;
  display: flex;
  flex-direction: column;
  align-items: center;
  transition: all 0.5s ease;
  box-shadow: 0 8rpx 20rpx rgba(0, 0, 0, 0.15);
  cursor: pointer;
}

.game-card:hover {
  transform: scale(1.05);
}

.card-image {
  width: 160rpx;
  height: 160rpx;
  border-radius: 16rpx;
  margin-top: 20rpx;
}

.card-title {
  margin-top: 20rpx;
  font-size: 28rpx;

  font-weight: bold;
}
</style>
